
import 'package:flutter/cupertino.dart';
import 'package:visibility_detector/visibility_detector.dart';

class DnpLifecycleWidget extends StatelessWidget {

  final Widget subWidget;

  final BuildContext? ctx;

  final Function() show;

  const DnpLifecycleWidget({
    super.key,
    required this.subWidget,
    required this.show,
    this.ctx,
  });

  @override
  Widget build(BuildContext context) {
    return VisibilityDetector(
      key:Key(ctx!.widget.runtimeType.toString()),
      onVisibilityChanged:_lifecycleChanged,
      child: subWidget,
    );
  }

  _lifecycleChanged(VisibilityInfo visibilityInfo) {
    var visiblePercentage = visibilityInfo.visibleFraction * 100;
    if (visiblePercentage == 100) {
      show();
    }
  }
}
